package com.mjk.common.core.jdbc.ibatis.typehandler.handler;

import com.mjk.common.core.jdbc.ibatis.typehandler.BaseHandler;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 本地时间适配器 格式为yyyy-MM-dd
 */
public class LocalDateHandler implements BaseHandler<LocalDate> {
    @Override
    public String resovleToString(Field f, LocalDate localDate) {
        if (ObjectUtils.isEmpty(localDate)) {
            return null;
        }
        return localDate.format(DateTimeFormatter.ofPattern(TimeUtils.DATE_FORMAT_DATEONLY));
    }

    @Override
    public LocalDate resovleResult(Object o) {
        return LocalDate.ofInstant(TimeUtils.parse(o.toString()).toInstant(), ZoneId.systemDefault());
    }
}
